package DAO;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import bean.Masterbean;
import bean.Petbean;
import bean.Userbean;
import conn.ConnectDatabase;
import service.IMasterDao;
import service.IUserDao;

public class MasterDaoImp implements IMasterDao {
	 PreparedStatement ps = null;
	 Date date = new Date();
		//得到一个timestamp格式的时间，存入mysql中的时间格式为"yyyy-MM-dd HH:mm:ss"
		Timestamp timestamp = new Timestamp(date.getTime());
	@Override
	public boolean insert(Masterbean p) {
		// TODO 自动生成的方法存根
		String sql ="insert into master(id,name,pet_id,tel,adress) values(?,?,?,?,?)";
		String sql1= "insert into history(description,data) values(?,?)";
		int n =0;
		try{
		ps = ConnectDatabase.getConnection().prepareStatement(sql);
		
		ps.setInt(1, p.getId());
		ps.setString(2, p.getName());
		ps.setInt(3, p.getPet_id());
		ps.setInt(4, p.getTel());
		ps.setString(5, p.getAdress());
		n=ps.executeUpdate();
		
		ps = ConnectDatabase.getConnection().prepareStatement(sql1);
		ps.setString(1, "增加了一条客户信息");
		ps.setTimestamp(2,timestamp);
		ps.executeUpdate();
		}catch (Exception e) {
		e.printStackTrace();
		}
		return n>0;
	}

	@Override
	public boolean delete(Masterbean p) {
		// TODO 自动生成的方法存根
		String sql ="delete from master where id="+p.getId();
		String sql1= "insert into history(description,data) values(?,?)";
		int n=0;
		try{
		ps=ConnectDatabase.getConnection().prepareStatement(sql);
		n=ps.executeUpdate();
		ps = ConnectDatabase.getConnection().prepareStatement(sql1);
		ps.setString(1, "删除了一条客户信息");
		ps.setTimestamp(2,timestamp);
		ps.executeUpdate();
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return n>0;
	}

	@Override
	public boolean update(Masterbean p) {
		// TODO 自动生成的方法存根
		String sql = "update master set name=?,pet_id=?,tel=?,adress=? where id=?";
		String sql1= "insert into history(description,data) values(?,?)";
		int n = 0;
		try{
		
		ps=ConnectDatabase.getConnection().prepareStatement(sql);
		ps.setString(1, p.getName());
		ps.setInt(2, p.getPet_id());
		ps.setInt(3, p.getTel());
		ps.setString(4, p.getAdress());
		ps.setInt(5, p.getId());
		n=ps.executeUpdate();
		
		ps = ConnectDatabase.getConnection().prepareStatement(sql1);
		ps.setString(1, "更新了一条客户信息");
		ps.setTimestamp(2,timestamp);
		ps.executeUpdate();
		}
		catch(Exception e){
			e.printStackTrace();
		}
		return n>0;
	}

	@Override
	public List<Masterbean> queryMaster(Map<String, Object> map) {
		// TODO 自动生成的方法存根
		StringBuffer sql= new StringBuffer("select * from master where 1 =1");

		List<Masterbean> list = new ArrayList<Masterbean>();
		try{
		ps = ConnectDatabase.getConnection().prepareStatement(sql.toString());
		ResultSet rs = ps.executeQuery();
		while(rs.next()){
			Masterbean p = new Masterbean();
			p.setId(rs.getInt("id"));
			p.setName(rs.getString("name"));
			p.setPet_id(rs.getInt("pet_id"));
			p.setTel(rs.getInt("tel"));
			p.setAdress(rs.getString("adress"));
			list.add(p);
			
	
		
		}
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		return list;
	}

	
	
	@Override
	public Masterbean queryById(Masterbean p) {
		Masterbean pet = null;
		String sql = "select * from master where id="+p.getId();
		try{
		ps = ConnectDatabase.getConnection().prepareStatement(sql);
		ResultSet rs = ps.executeQuery();
		if(rs.next()){
			pet = new Masterbean();
			pet.setId(rs.getInt("id"));
			pet.setName(rs.getString("name"));
			pet.setPet_id(rs.getInt("pet_id"));
			pet.setTel(rs.getInt("tel"));
			pet.setAdress(rs.getString("adress"));
					
					
		}
		}catch(Exception e){
			e.printStackTrace();
		}
		return pet;
	}



}
